package com.onlyphoto.onlymall.mvp.view.activity

import android.content.Intent
import android.graphics.Color
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.onlyphoto.onlymall.R
import com.onlyphoto.onlymall.base.BaseActivity
import com.onlyphoto.onlymall.bean.response.CouponBean
import com.onlyphoto.onlymall.events.ReceiveEvent
import com.onlyphoto.onlymall.mvp.contract.CCoupon
import com.onlyphoto.onlymall.mvp.presenter.PCouponImpl
import com.onlyphoto.onlymall.mvp.view.binder.CouponBinder
import com.onlyphoto.onlymall.utils.NetWorkStatus
import com.onlyphoto.onlymall.utils.ToastUtil
import kotlinx.android.synthetic.main.activity_coupon.*
import me.bakumon.statuslayoutmanager.library.DefaultOnStatusChildClickListener
import me.bakumon.statuslayoutmanager.library.StatusLayoutManager

import me.drakeet.multitype.Items
import me.drakeet.multitype.MultiTypeAdapter
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode


class CouponActivity : BaseActivity<PCouponImpl>(), CCoupon.IVCoupon {

    private var layoutManager: LinearLayoutManager? = null
    private var adapter: MultiTypeAdapter? = null
    var items: Items? = null
    var isStore = false

    override fun retry() {}

    override fun init() {
        isStore = intent.getBooleanExtra("isStore", false)
        layoutManager = LinearLayoutManager(this)
        adapter = MultiTypeAdapter()
        items = Items()
        adapter?.register(CouponBean::class.java, CouponBinder())
        adapter?.items = items!!
        rv_coupon.adapter = adapter
        rv_coupon.layoutManager = layoutManager
    }

    override fun initListener() {
        my_coupons.setOnClickListener{
            startActivity(Intent(this, CouponActivity::class.java))
        }
        back.setOnClickListener{
            finish()
        }
    }

    override fun loadData() {
        if (isStore) {
            mPresenter?.getStoreCoupons(1)
            tv_title.text = "店铺优惠"
            my_coupons.visibility = View.VISIBLE
        } else {
            mPresenter?.getUserCoupons(2, 0)
        }
    }

    override fun getCouponsSuccess(list: ArrayList<CouponBean>) {
        if (items?.size!! > 0) {
            items?.clear()
        }
        if (isStore) {
            for (bean in list) {
                bean.isStore = true
            }
        }
        items?.addAll(list)
        adapter?.notifyDataSetChanged()
    }

    override fun receiveCouponSuccess() {
    }

    override fun showToast(msg: String) {
        ToastUtil.showToast(msg)
    }

    override fun isFullScreen(): Boolean {
        //是否沉浸式， 默认非沉浸式
        return false
    }

    override fun showEmpty() {
        commonStatusView!!.setContent(R.mipmap.coupon_empty, "暂无优惠券～", "")
        commonStatusView!!.setType(NetWorkStatus.EMPTY)
        showCustomLayout(commonStatusView!!, R.id.ll_content)
    }

    public override fun initCustomerStatusManager() {
        builder = StatusLayoutManager.Builder(rv_coupon)
                .setDefaultLoadingText("数据加载中...")
                .setOnStatusChildClickListener(object : DefaultOnStatusChildClickListener() {
                    override fun onCustomerChildClick(view: View) {
                        super.onCustomerChildClick(view)
                        when (view.id) {
                            R.id.ll_content -> {
                                when {
                                    view.tag == NetWorkStatus.FAIL -> retry()
                                    view.tag == NetWorkStatus.ERROR -> retry()
                                    view.tag == NetWorkStatus.EMPTY -> {
                                    }
                                }
                            }
                            else -> retry()
                        }
                    }
                })
                .setDefaultLayoutsBackgroundColor(Color.WHITE)
        statusLayoutManager = builder!!.build()
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMessageEvent(event: ReceiveEvent) {
        mPresenter?.receiveCoupon(event.couponId, event.endTm)
    }

    override fun setContentViewId(): Int {
        return R.layout.activity_coupon
    }

    override fun createPresenter() {
        mPresenter = PCouponImpl(mContext, this)
    }

    override fun showLoading() {

    }

    override fun hideLoading() {

    }

}
